
var DEBUG = true;
var DURATION = 80;
var all = ['#action_ask #confirm', '#action_ask #send', '#action_ask #answer'];

function debug(msg) {
	if(DEBUG) {
		$('#debug').append(msg+'<br />');
	}
}

function show(components) {
	for(var c in all) {
		if($.inArray(all[c], components) > -1) {
			$(all[c]).show();
		}
		else {
			$(all[c]).hide();
		}
	}
}

function clearAll() {
	show([]);
	$('#action_ask .hint').hide();
	$('#action_ask #response').empty();
	$('#action_ask #submit').removeAttr('disabled');
	$('#action_ask #submit').css('color', '#FFFFFF');
	$('#action_ask #question').removeAttr('disabled');
	$('#action_ask #question').css('color', '#36393D');
	$('#action_ask #send p b').empty();
	$('#action_ask #answer p').empty();
}

$(document).ready(function() {
	show([]);
	$('#action_ask .hint').hide();
	
	$('#action_ask #submit').click( function() {
		$('#action_ask .hint').hide();
		var question = $('#action_ask #question').val();
		if(question.length < 20) {
			$('#action_ask #ask_warning').text('Warning: your question is too short.');
			$('#action_ask #ask_warning').show();
		} else if(question.length > 2048) {
			$('#action_ask #ask_warning').text('Warning: your question is too long.');
			$('#action_ask #ask_warning').show();
		} else {
			$('#action_ask #response').load(
				'submit',
				{ q: question },
				function() {
					var category = $('#action_ask #response #para_category').text();
					// TODO check if category (server response) is valid.  
					$('#action_ask #confirm  #_'+category).attr('selected', 'selected'); 
					$('#action_ask #submit').attr('disabled', 'disabled');
					$('#action_ask #submit').css('color', '#333333');
					$('#action_ask #question').attr('disabled', 'disabled');
					$('#action_ask #question').css('color', '#666666');
					show(['#action_ask #confirm']);
				}
			);
		}
	});

	$('#action_ask #cancel').click( function() {
		clearAll();
	});

	$('#action_ask #confirm_yes').click ( function() {
		$('#action_ask .hint').hide();
		var question = $('#action_ask #question').val();
		var category = "";
		$('#action_ask #confirm select option:selected').each(
			function() { category = $(this).text(); }
		);
		$('#action_ask #response').load(
			'confirm',
			{ q: question, c: category },
			function() {
				if($('#action_ask #response #para_number').length != 0) {
					var number = $('#action_ask #response #para_number').text();
					$('#action_ask #send p b').text(number); 
					show(['#action_ask #send']);
				} else if($('#action_ask #response #para_answer').length != 0) {
					var answer = $('#action_ask #response #para_answer').text();
					$('#action_ask #answer p').text(answer);
					show(['#action_ask #answer']);
				} else {
					debug('quirk');
					// TODO quirk mode...
				}
			}
		);
	});

	$('#action_ask #next').click ( function() {
		clearAll();
	});
	
	$('#action_ask #helpful_yes').click ( function() {
		$('#action_ask .hint').hide();
		var question = $('#action_ask #question').val();
		var category = "";
		$('#action_ask #confirm select option:selected').each(
			function() { category = $(this).text(); }
		);
		var answer = $('#action_ask #answer p').text();
		$('#action_ask #response').load(
			'helpful',
			{ q: question, c: category, a: answer }, 
			function() {
				clearAll();
			}
		);
	});
	
	$('#action_ask #helpful_no').click ( function() {
		$('#action_ask .hint').hide();
		var question = $('#action_ask #question').val();
		var category = "";
		$('#action_ask #confirm select option:selected').each(
			function() { category = $(this).text(); }
		);
		$('#action_ask #response').load(
			'badAnswer',
			{ q: question, c: category },
			function() {
				var number = $('#action_ask #response #para_number').text();
				$('#action_ask #send p b').text(number); 
				show(['#action_ask #send']);
			}
		);
	});
});
